package test_pkg

type Sorter interface {
	Len() int
	Less(i, j int) bool
	Swap(i, j int)
}

func Sort(data Sorter) {
	len := data.Len()
	if len <= 1 {
		return
	}

	for i := 1; i < len; i++ {
		for j := 0; j < len-i; j++ {
			if data.Less(j, j+1) { //倒排  j+1, j 顺排
				data.Swap(j, j+1)
			}
		}
	}
}

// 数字排序
type IntArray []int

func (a IntArray) Len() int           { return len(a) }
func (a IntArray) Less(i, j int) bool { return a[i] < a[j] }
func (a IntArray) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func SortInts(a []int) { Sort(IntArray(a)) }

// 字符串排序
type StringArray []string

func (a StringArray) Len() int           { return len(a) }
func (a StringArray) Less(i, j int) bool { return a[i] < a[j] }
func (a StringArray) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func SortStrings(a []string) { Sort(StringArray(a)) }

type day struct {
	num       int
	shortName string
	longName  string
}

type dayArray struct {
	data []*day
}

func (p *dayArray) Len() int           { return len(p.data) }
func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
func (p *dayArray) Swap(i, j int)      { p.data[i], p.data[j] = p.data[j], p.data[i] }
